//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.Generic;
using System.Diagnostics;
using OpenLS.Spreadsheet.Collections;
using System.Diagnostics.Contracts;

namespace OpenLS.Spreadsheet.Expressions
{
    static class SharedExpressionHelper
    {
        internal static Collections.HashSet<SharedExpression> GetSharedFormulas(Range range)
        {
            return GetSharedFormulas(range.GetNonEmptyCells());
        }
        internal static Collections.HashSet<SharedExpression> GetSharedFormulas(IEnumerable<Cell> cells )
        {
            Collections.HashSet<SharedExpression> result = new Collections.HashSet<SharedExpression>();
            foreach (Cell cell in cells)
            {
                SharedExpressionReference exp = cell.Expression as SharedExpressionReference;
                if (exp == null)
                    continue;
                if (result.Contains(exp.SharedExpression))
                    continue;
                result.Add(exp.SharedExpression);
            }
            return result;
        }
        internal static void Unshare(Cell cell)
        {
            SharedExpressionReference sharedRef = cell.Expression as SharedExpressionReference;
            Contract.Assert(sharedRef != null);
            cell.Expression = sharedRef.ActualExpression;
        }
    }
}
